package com.kangyi.admin.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kangyi.admin.mapper.AdminUserMapper;
import com.kangyi.admin.mapper.ReviewImageMapper;
import com.kangyi.admin.mapper.ReviewMapper;
import com.kangyi.admin.mapper.ReviewReplyMapper;
import com.kangyi.admin.service.ReviewService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.context.BaseContext;
import com.kangyi.dto.AddReviewReplyDTO;
import com.kangyi.dto.ReviewPageDTO;
import com.kangyi.entity.NursingHomeReviewReply;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.exception.PermissionsException;
import com.kangyi.exception.ToExamineException;
import com.kangyi.result.PageResult;
import com.kangyi.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private ReviewMapper reviewMapper;

    @Autowired
    private ReviewReplyMapper reviewReplyMapper;

    @Autowired
    private ReviewImageMapper reviewImageMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    // 调用后检查当前线程id是否有足够的权限进行后续操作
    public void validateUserPermissions() {
        Integer currentId = BaseContext.getCurrentId();
        AdminUserVO adminUserVO = adminUserMapper.selectById(currentId);
        // 判断权限是否既不是1也不是2，如果是则抛出异常
        if (!adminUserVO.getPermissions().equals(1) && !adminUserVO.getPermissions().equals(2)) {
            throw new PermissionsException(MessageConstant.INSUFFICIENT_PRIVILEGE);
        }
    }

    @Override
    public PageResult pageQuery(ReviewPageDTO reviewPageDTO) {
        validateUserPermissions();
        PageHelper.startPage(reviewPageDTO.getPage(),reviewPageDTO.getPageSize());
        Page<ReviewPageQueryVO> page = reviewMapper.pageQuery(reviewPageDTO);
        if (CollUtil.isEmpty(page.getResult())) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        long total = page.getTotal();
        List<ReviewPageQueryVO> result = page.getResult();
        for (ReviewPageQueryVO vo : result) {
            String phone = vo.getPhone();
            if (phone != null && phone.length() >= 6) {
                vo.setPhone(phone.substring(0, phone.length() - 6) + "******");
            }
            Integer id = vo.getId();
            NursingHomeReviewReplyVO byReviewId = reviewReplyMapper.getByReviewId(id);
            vo.setIsIsDeleted(byReviewId != null);
        }
        return new PageResult(total,result);
    }
    @Override
    public void replyReview(AddReviewReplyDTO addReviewReplyDTO) {
        validateUserPermissions();

        NursingHomeReviewReplyVO byReviewId = reviewReplyMapper.getByReviewId(addReviewReplyDTO.getReviewId());
        if (byReviewId != null) {
           throw new ToExamineException(MessageConstant.DO_NOT_REPEAT_THE_OPERATION);
        }

        NursingHomeReviewReply nursingHomeReviewReply = new NursingHomeReviewReply();
        BeanUtil.copyProperties(addReviewReplyDTO, nursingHomeReviewReply);
        reviewMapper.replyReview(nursingHomeReviewReply);
    }

    @Override
    public ReviewDetailVO detailReview(Integer reviewId) {
        validateUserPermissions();

        ReviewDetailVO reviewDetailVO = reviewMapper.detailReview(reviewId);
        if (reviewDetailVO == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }

        List<NursingHomeReviewImageVO> nursingHomeReviewImageVOList = reviewImageMapper.getByReviewId(reviewId);
        reviewDetailVO.setNursingHomeReviewImageVOList(nursingHomeReviewImageVOList);

        NursingHomeReviewReplyVO nursingHomeReviewReplyVO = reviewReplyMapper.getByReviewId(reviewId);
        reviewDetailVO.setIsDeleted(nursingHomeReviewReplyVO != null);
        reviewDetailVO.setNursingHomeReviewReplyVO(nursingHomeReviewReplyVO);

        String phone = reviewDetailVO.getPhone();
        if (phone != null && phone.length() >= 6) {
            reviewDetailVO.setPhone(phone.substring(0, phone.length() - 6) + "******");
        }
        return reviewDetailVO;
    }

    @Override
    public void deleteReply(Integer replyId) {
        validateUserPermissions();
        reviewReplyMapper.deleteByReplyId(replyId);
    }
}
